package servlet;

import model.Essay;
import model.User;
import util.DBUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ShowEssayServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入ShowEssay");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int eid=Integer.parseInt(request.getParameter("fromeid"));
        System.out.println("ShowEssay:eid:"+eid);


        Essay e=new Essay();
        List<String[]> essayLabelList=new ArrayList();



            try {
                Connection conn=null;
                PreparedStatement ptmt=null;
                ResultSet rs=null;
                Class.forName("com.mysql.cj.jdbc.Driver");//注册数据库驱动
                String url = "jdbc:mysql://localhost:3306/boke?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf8";
                conn= DriverManager.getConnection(url, "root", "123456");//通过DriverManager获取数据库
                String  sql2="update essay\n" +
                        "set read_sum=read_sum+1\n" +
                        "where eid=?";
                ptmt=conn.prepareStatement(sql2);
                ptmt.setInt(1,eid);
                int a= ptmt.executeUpdate();
                if(a==0){
                    System.out.println("阅读量添加失败");
                }else {
                    System.out.println("阅读量添加成功");
                }
                ////////////////////////////////////////////////////////////////////////////////////////////
                String sql="select e.*,u.username\n" +
                        "from essay e join `user` u\n" +
                        "on e.uid=u.uid\n  where eid=?";
                ptmt=conn.prepareStatement(sql);
                ptmt.setInt(1,eid);
                rs = ptmt.executeQuery();
                if(rs==null){
                    System.out.println("文章数据加载失败");
                    request.getSession().setAttribute("status","0");
                    request.getSession().setAttribute("msg","文章数据加载失败");
                }else {
                    rs.next();
                    e.setEid(eid);
                    e.setUid(rs.getInt("uid"));
                    e.setAuther(rs.getString("username"));
                    e.setTitle(rs.getString("title"));
                    e.setEclass(rs.getString("eclass"));
                    e.setCreate_time(rs.getTimestamp("create_time"));
                    e.setContent(rs.getString("content"));
                    e.setRead_sum(rs.getInt("read_sum"));
                    e.setZan_sum(rs.getInt("zan_sum"));
                }
                //////////////////////////////////////////////////////////////////////////////
                String  sql1="SELECT e.eid,l.lid,l.word\n" +
                        "FROM le e JOIN label l\n" +
                        "on l.lid=e.lid where eid=?";
                ptmt=conn.prepareStatement(sql1);
                ptmt.setInt(1,eid);
                rs = ptmt.executeQuery();
                if(rs==null){
                    System.out.println("文章标签数据加载失败");
                    request.getSession().setAttribute("status","0");
                    request.getSession().setAttribute("msg","文章的标签数据加载失败");
                }else {
                    while (rs.next()){
                        String[] data=new String[3];
                        //System.out.println(rs.getObject(1)+"|"+rs.getObject(2)+"|"+rs.getObject(3));
                        for(int i=0;i<data.length;i++){
                            if(rs.getObject(i+1)==null){
                                data[i]="";
                            }else {
                                data[i]=rs.getObject(i+1).toString();
                            }
                        }
                        for (String s:
                                data) {
                            System.out.print("|"+s+"|");
                        }
                        System.out.println();
                        essayLabelList.add(data);
                        System.out.println("文章标签数据加载成功");

                    }
                }
                ////////////////////////////////////////////////////////////////////////////////////////////

                rs.close();
                ptmt.close();
                conn.close();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            request.getSession().setAttribute("essayLabelList",essayLabelList);
            request.getSession().setAttribute("essay",e);
           /* request.getSession().setAttribute("status","1");
            request.getSession().setAttribute("msg","文章加载成功");*/
            RequestDispatcher rd = request.getRequestDispatcher("essay.jsp");
            rd.forward(request,response);
        }




    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
